package com.nightpiter.view.dialogs;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.ViewInflate;
import android.widget.CheckBox;
import android.widget.LinearLayout;

import com.nightpiter.R;
import com.nightpiter.model.MetroModelState;

/**
 * @author Eldar Abusalimov (eldar.abusalimov@gmail.com)
 */
public class LoaderDialog implements OnClickListener {

	/** the dialog itself */
	private AlertDialog dialog;

	/** builder for the dialog */
	private AlertDialog.Builder builder;

	/** View to attach to the dialog */
	private ContentView contentView;

	/** if false do not show dialog after user pressed hide button */
	private boolean allowPopup = true;

	/**
	 * public constructor
	 * 
	 * @param context
	 */
	public LoaderDialog(Context context) {
		this.contentView = new ContentView(context);

		this.builder = new AlertDialog.Builder(context);
		this.builder.setTitle(R.string.loader_title);
		this.builder.setView(this.contentView);
		this.builder.setPositiveButton("Hide", this);

	}

	/**
	 * show current state of the model
	 * 
	 * @param modelState
	 */
	public void show(MetroModelState modelState) {
		this.contentView.showModelState(modelState);
		show();
	}

	/**
	 * indicate current state of the drawer
	 * 
	 * @param drawerState
	 */
	public void show(boolean drawerState) {
		this.contentView.showDrawerState(drawerState);
		show();
	}

	/**
	 * show the dialog if it has not already shown
	 */
	public void show() {
		if (this.dialog == null) {
			if (this.allowPopup) {
				this.dialog = builder.show();
			}
		}
	}

	/**
	 * hide the dialog if it is visible now
	 */
	public void hide() {
		if (this.dialog != null) {
			this.dialog.dismiss();
			this.dialog = null;
		}
		this.allowPopup = true;
	}

	/**
	 * handles button clicks
	 */
	public void onClick(DialogInterface dialog, int button) {
		if (dialog == this.dialog) {
			switch (button) {
			case DialogInterface.BUTTON1:
				this.allowPopup = false;
				break;

			default:
				break;
			}
			this.dialog = null;
		}
	}

	/**
	 * ContentView class.
	 * 
	 * @author Eldar Abusalimov (eldar.abusalimov@gmail.com)
	 */
	public class ContentView extends LinearLayout {

		/** checkBoxes that show the loading state */
		private CheckBox builtCheckBox, readCheckBox, drawerPrepareCheckBox;

		/**
		 * @param context
		 */
		public ContentView(Context context) {
			super(context);
			init();
		}

		/**
		 * linking to layout views
		 */
		private void init() {
			ViewInflate.from(getContext()).inflate(R.layout.loader, this, null);

			this.builtCheckBox = (CheckBox) findViewById(R.id.check_built);
			this.readCheckBox = (CheckBox) findViewById(R.id.check_read);
			this.drawerPrepareCheckBox = (CheckBox) findViewById(R.id.check_drawer);
		}

		/**
		 * update View according to the MetroModelState object
		 * 
		 * @param state
		 */
		private void showModelState(MetroModelState state) {
			this.builtCheckBox.setChecked(state.isBuilt());
			this.readCheckBox.setChecked(state.isRead());

			if (state.getAction() == MetroModelState.Action.BUILDING) {
				this.builtCheckBox.press(false);
				this.readCheckBox.unpress();
			} else if (state.getAction() == MetroModelState.Action.READING) {
				this.readCheckBox.press(false);
				this.builtCheckBox.unpress();
			}

			this.drawerPrepareCheckBox.setVisibility(GONE);
			this.builtCheckBox.setVisibility(VISIBLE);
			this.readCheckBox.setVisibility(VISIBLE);

		}

		/**
		 * indicate if drawer is prepared
		 * 
		 * @param complete
		 */
		private void showDrawerState(boolean complete) {
			this.drawerPrepareCheckBox.setChecked(complete);

			if (complete) {
				this.drawerPrepareCheckBox.unpress();
			} else {
				this.drawerPrepareCheckBox.press(false);
			}

			this.builtCheckBox.setVisibility(GONE);
			this.readCheckBox.setVisibility(GONE);
			this.drawerPrepareCheckBox.setVisibility(VISIBLE);

		}

	}

}
